package com.gexin.model.agent;

import com.gexin.common.utils.SQLUtils;
import com.gexin.common.utils.StringUtil;
import com.gexin.model.agent.base.BaseAgentCity;
import com.jfinal.plugin.activerecord.Page;

import java.util.List;

/**
 * 业务名称:  t_agent_city(代理商城市表) 
 */
@SuppressWarnings("serial")
public class AgentCity extends BaseAgentCity<AgentCity> {
	public static final AgentCity dao = new AgentCity();
	public static final Integer STATUS_FAIL = 2;
	public static final Integer STATUS_SUCCESS = 1;
	public static final Integer STATUS_NOTAUDIT = 0;




	public static final Integer TYPE_AREA = 1;
	public static final Integer TYPE_CITY = 0;

	/**
	 * 根据代理商id和城市id查询是否有代理商
	 * @param agentId
	 * @param cityid
	 * @return
	 */
	public AgentCity findByCityIdAndAgentId(Integer agentId , String cityid) {
		return	findFirst("select * from t_agent_city where agent_id=? AND city_id=?",agentId,cityid);
	}

	/**
	 * 根据代理商id和区域id查询是否有代理商
	 * @param agentId
	 * @param cityid
	 * @return
	 */
	public AgentCity findByAgentIdAndAreaId(Integer agentId , String areaId) {
		return	findFirst("select * from t_agent_city where agent_id=? AND area_id=?",agentId,areaId);
	}

	public List<AgentCity> findByAgentId(Integer agentId,Integer status) {
		SQLUtils sql = new SQLUtils();
		sql.append("select tac.*,addrCity.city as cityName,level.name as levelName,level.discount as discount,area.area as areaName from t_agent_city tac " );
		sql.append(" LEFT JOIN t_addr_cities addrCity on tac.city_id=addrCity.cityid " );
		sql.append(" LEFT JOIN t_addr_areas area on tac.area_id=area.areaid " );
		sql.append(" LEFT JOIN t_agent_level level on level.id=tac.level_id " );
		sql.append(" where tac.agent_id=? ");
		if(status!=null){
			sql.whereEquals("tac.status",status);
		}
		return	find(sql.toString(),agentId);
	}

	public List<AgentCity> findCityByAgentId(String id) {

		return find("select tac.id,tac.city_id,addrCity.city as cityName from t_agent_city tac LEFT JOIN t_addr_cities addrCity on tac.city_id=addrCity.cityid  where tac.agent_id=?",id);
	}

	public Page<AgentCity> findAgentAuditList(int page, int pageSize, String name, String phone, String type)
	{
		SQLUtils sql = new SQLUtils();
		sql.append(" FROM t_agent agent ");
		sql.append(" LEFT JOIN t_agent_city agentCity ON agent.id=agentCity.agent_id ");
		sql.append(" LEFT JOIN t_addr_cities city ON agentCity.city_id = city.cityid ");
		sql.append(" LEFT JOIN t_addr_areas area ON agentCity.area_id = area.areaid ");
		sql.append(" where ((agentCity.`status` = 0 AND agent.type=0)  OR (agent.type<>0 and agent.`status`=0))");
		if (!StringUtil.isBlank(name)) {
			sql.whereLike("agent.agent_name", name);
		}
		if (!StringUtil.isBlank(phone)) {
			sql.whereLike("agent.agent_phone", phone);
		}
		if (!StringUtil.isBlank(type)) {
			sql.whereLike("agent.type", type);
		}
		sql.append(" order by agent.modified_time desc ");

		StringBuffer select = new StringBuffer();
		select.append(" SELECT agentCity.*,");
		select.append(" agent.level_id as level_id,");
		select.append(" agent.id as agentId,agent.status as agentStatus,");
		select.append(" agent.agent_name AS agentName,agent.modified_time AS aduitTime,");
		select.append(" agent.agent_phone,agent.address as address,");
		select.append(" agent.agent_boss_name as agent_boss_name,agent.business_license as business_license,");
		select.append(" agent.logo_img as logo_img,agent.cash_deposit_state,");
		select.append(" agent.cash_deposit,agent.control_moeny_state,");
		select.append(" agent.control_money_amount,agent.send_mail, ");
		select.append(" city.city AS cityName, ");
		select.append(" agent.type AS agentType, ");
		select.append(" area.area AS areaName ");

		return paginate(page, pageSize, select.toString(), sql.toString());
	}
	/**
	 * 根据等级id查询
	 * @param agentLevelId
	 */
	public List<AgentCity> findByLevelId(String agentLevelId) {
		SQLUtils sql = new SQLUtils();
		sql.append("select * from t_agent_city where level_id=?");
		return find(sql.toString(),agentLevelId);
	}

	public AgentCity findByCityId(Integer cityid) {
		return	findFirst("select * from t_agent_city where  city_id=?",cityid);

	}
}
